package com.yanqu.road.server.logic.season.washe;

import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.activity.washe.config.WasheConfig;
import com.yanqu.road.entity.activity.washe.config.WasheSeasonConfig;
import com.yanqu.road.entity.activity.washe.data.WasheUserData;
import com.yanqu.road.entity.activity.washe.data.WasheUserJinBiaoData;
import com.yanqu.road.entity.enums.eSeasonRewardType;
import com.yanqu.road.entity.enums.eSystemId;
import com.yanqu.road.entity.log.LogWasheSeasonReward;
import com.yanqu.road.entity.season.SeasonRewardConfig;
import com.yanqu.road.entity.season.SeasonRewardData;
import com.yanqu.road.logic.helper.SeasonHelper;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.washe.WasheModule;
import com.yanqu.road.server.logic.season.BaseSeasonRank;
import com.yanqu.road.server.manger.activity.washe.WasheMgr;
import com.yanqu.road.server.manger.log.AutoLogMgr;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.utils.date.DateHelper;

import java.util.concurrent.ConcurrentHashMap;

public class WasheJinBiaoScoreRankCondition extends BaseSeasonRank {

    private WasheSeasonConfig seasonConfig;

    public WasheJinBiaoScoreRankCondition(eSeasonRewardType type, GamePlayer player) {
        super(type, player);
    }

    @Override
    protected void init() {
        WasheConfig washeConfig = WasheMgr.getWasheConfig();
        if (washeConfig != null) {
            seasonConfig = washeConfig.getSeasonConfigMap().get(seasonId);
        }
    }

    @Override
    protected boolean canChange(Object param) {
        if (seasonConfig == null) {
            return false;
        }
        long now = System.currentTimeMillis();
        if (seasonConfig.getBeginTime() > now || now > seasonConfig.getEndTime()) {
            return false;
        }
        if (seasonConfig.getIntegralBeginTime() > now || now > seasonConfig.getIntegralEndTime()) {
            return false;
        }
        // 判断下积分有没有变动，0不上榜
        if (data.getValue() == (long) param) {
            return false;
        }
        return true;
    }

    @Override
    public boolean canChangeRank() {
        WasheConfig config = WasheMgr.getWasheConfig();
        WasheUserJinBiaoData jinBiaoData = player.getModule(WasheModule.class).getWasheUserJinBiaoData();
        if (config != null && jinBiaoData != null) {
            return true;
        }
        return false;
    }

    @Override
    protected void changeValue(Object param) {
        long value = (long) param;
        data.setValue(value);
    }

    @Override
    protected int initSystemId() {
        return eSystemId.WasheActivity.getValue();
    }

    @Override
    protected int initSeasonId() {
        WasheConfig washeConfig = WasheMgr.getWasheConfig();
        if (washeConfig == null) {
            return WasheConfig.SEASON_ID_NULL;
        }
        WasheSeasonConfig config = washeConfig.getWasheSeasonConfig();
        if (config == null) {
            return WasheConfig.SEASON_ID_NULL;
        }
        return config.getSeasonId();
    }

    @Override
    protected String initRangeKey() {
        WasheConfig washeConfig = WasheMgr.getWasheConfig();
        if (washeConfig == null) {
            return "";
        }
        ActivityInfo activityInfo = washeConfig.getActivityInfo();
        return SeasonHelper.getWasheRangeKey(activityInfo.getParam6());
    }

    @Override
    public int inGetRewardTime() {
        if (seasonConfig != null) {
            long now = System.currentTimeMillis();
            if (seasonConfig.getEndTime() + DateHelper.MINUTE_MILLIONS * 5 <= now && now < seasonConfig.getEndShowTime()) {
                return 0;
            }
            if (seasonConfig.getEndTime() <= now && now < seasonConfig.getEndTime() * DateHelper.MINUTE_MILLIONS * 5) {
                return GameErrorCode.E_SEASON_REWARD_IN_REWARD_SETTLE_TIME;
            }
        }
        return GameErrorCode.E_SEASON_REWARD_NO_GET_REWARD_TIME;
    }

    @Override
    protected void addLog(SeasonRewardConfig config, SeasonRewardData data) {
        long seasonPlayTimes = 0;
        long seasonPlayTime = 0;
        WasheUserData userData = player.getModule(WasheModule.class).getWasheUserData();
        if (userData != null) {
            seasonPlayTimes = userData.getPlayTimes();
            seasonPlayTime = userData.getSeasonPlayTime();
        }

        AutoLogMgr.add(new LogWasheSeasonReward(
                data.getUserId(),
                config.getType(),
                config.getSeasonId(),
                config.getRewardId(),
                seasonPlayTimes,
                seasonPlayTime
        ));
    }
}
